package com.gl.doc.operation.comment.bindcomment;

import cn.hutool.core.util.ObjectUtil;
import com.gl.doc.builder.WordDocument;
import com.gl.doc.model.Paragraph;
import com.gl.doc.model.SubParagraph;
import com.gl.doc.model.Word;
import com.gl.doc.model.WordComment;
import com.gl.doc.operation.comment.entity.TargetSentenceIndex;
import com.gl.doc.operation.comment.entity.TargetSubParagraphContentIndex;
import com.gl.doc.operation.comment.entity.WordCommentRelation;
import com.gl.doc.operation.comment.impl.DefaultBindComment;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 第二种段落里面有两个以上子段落相关判断
 */
public class CaseSecondBindComment extends DefaultBindComment {
    /**
     * 依赖于第一种情况
     */
    private final DefaultBindComment defaultBindComment = new CaseOneBindComment();

    @Override
    public void doAddParagraphComment(WordCommentRelation wordCommentRelation) {
        List<TargetSubParagraphContentIndex> targetSubParagraphContentIndexList = wordCommentRelation.getTargetSubParagraphContentIndexList();
        if (targetSubParagraphContentIndexList.size() > 1) {
            TargetSentenceIndex targetSentenceIndex = wordCommentRelation.getTargetSentenceIndex();
            Integer startIndex = targetSentenceIndex.getStartIndex();
            Integer endIndex = targetSentenceIndex.getEndIndex();
            //index 用于记录多个子段落中出现需要批注句子的下一次子段落坐标，这样可以避免一句批注句子出现在多个字段导致每次获取的子段落重复。
            int index = 0;
            List<TargetSubParagraphContentIndex> tempTargetSubParagraphContentIndexList = new ArrayList<>();
            for (TargetSubParagraphContentIndex targetSubParagraphContentIndex : targetSubParagraphContentIndexList) {
                index++;
                Integer targetStartIndex = targetSubParagraphContentIndex.getStartIndex();
                Integer targetEndIndex = targetSubParagraphContentIndex.getEndIndex();
                if ((startIndex >= targetStartIndex && startIndex <= targetEndIndex) && endIndex <= targetEndIndex) {
                    wordCommentRelation.setTargetSubParagraphContentIndexList(Collections.singletonList(targetSubParagraphContentIndex));
                    defaultBindComment.doAddParagraphComment(wordCommentRelation);
                    break;
                } else if (startIndex >= targetStartIndex && targetEndIndex >= startIndex) {
                    tempTargetSubParagraphContentIndexList.add(targetSubParagraphContentIndex);
                    for (int i = index; i < targetSubParagraphContentIndexList.size(); i++) {
                        TargetSubParagraphContentIndex subParagraphContentIndex = targetSubParagraphContentIndexList.get(i);
                        if (endIndex >= subParagraphContentIndex.getStartIndex()) {
                            tempTargetSubParagraphContentIndexList.add(subParagraphContentIndex);
                        }
                    }
                }
            }
            this.doAddParagraphComment(wordCommentRelation, tempTargetSubParagraphContentIndexList);
        }
    }

    /**
     * 添加段落批注
     *
     * @param wordCommentRelation                    批注关系
     * @param tempTargetSubParagraphContentIndexList 被添加批注句子出现的段落坐标
     */
    private void doAddParagraphComment(WordCommentRelation wordCommentRelation, List<TargetSubParagraphContentIndex> tempTargetSubParagraphContentIndexList) {
        if (ObjectUtil.isEmpty(tempTargetSubParagraphContentIndexList)) {
            return;
        }
        WordComment wordComment = wordCommentRelation.getWordComment();
        WordDocument wordDocument = wordCommentRelation.getWordDocument();
        Word word = wordDocument.getWord();
        File commentFile = new File(word.getCommentsPath());
        Paragraph targetParagraph = wordComment.getTargetParagraph();
        Element element = targetParagraph.getElement();
        List<Element> elements = element.elements();
        TargetSentenceIndex targetSentenceIndex = wordCommentRelation.getTargetSentenceIndex();
        Integer startIndex = targetSentenceIndex.getStartIndex();
        Integer endIndex = targetSentenceIndex.getEndIndex();
        //批注句子出现的第一个子段落的坐标位置
        TargetSubParagraphContentIndex first = tempTargetSubParagraphContentIndexList.get(0);
        Integer firstStartIndex = first.getStartIndex();
        Integer firstEndIndex = first.getEndIndex();
        //批注句子出现的最后一个子段落的坐标位置
        TargetSubParagraphContentIndex last = tempTargetSubParagraphContentIndexList.get(tempTargetSubParagraphContentIndexList.size() - 1);
        Integer lastStartIndex = last.getStartIndex();
        Integer lastEndIndex = last.getEndIndex();
        SubParagraph firstSubParagraph = first.getSubParagraph();
        SubParagraph lastSubParagraph = last.getSubParagraph();
        //开始在第一个字符和结束最后一个字符相等
        if ((startIndex.equals(firstStartIndex) || firstStartIndex.equals(firstEndIndex))
                && (endIndex.equals(lastEndIndex) || lastStartIndex.equals(lastEndIndex))
        ) {
            super.addComment(wordComment, commentFile);
            super.doBindComment(firstSubParagraph.getRIndex(), lastSubParagraph.getRIndex() + 2, lastSubParagraph.getRIndex() + 3, elements, wordComment);
        } else if (startIndex > firstStartIndex && (endIndex.equals(lastEndIndex) || lastStartIndex.equals(lastEndIndex))) {
            this.leftParagraph(targetParagraph, elements, startIndex, firstStartIndex, firstEndIndex, firstSubParagraph);
            super.addComment(wordComment, commentFile);
            super.doBindComment(firstSubParagraph.getRIndex() + 1, lastSubParagraph.getRIndex() + 3, lastSubParagraph.getRIndex() + 4, elements, wordComment);
        } else if (endIndex < lastEndIndex && (startIndex.equals(firstStartIndex) || firstStartIndex.equals(firstEndIndex))) {
            rightParagraph(targetParagraph, elements, endIndex, lastStartIndex, lastEndIndex, lastSubParagraph);
            super.addComment(wordComment, commentFile);
            super.doBindComment(firstSubParagraph.getRIndex(), lastSubParagraph.getRIndex() + 2, lastSubParagraph.getRIndex() + 3, elements, wordComment);
        } else if (startIndex > firstStartIndex && endIndex < lastEndIndex) {
            this.leftParagraph(targetParagraph, elements, startIndex, firstStartIndex, firstEndIndex, firstSubParagraph);
            String targetParagraphContent = targetParagraph.getContent();
            String leftContent = targetParagraphContent.substring(lastStartIndex, endIndex + 1);
            String rightContent = targetParagraphContent.substring(endIndex + 1, lastEndIndex + 1);
            Element tElement = lastSubParagraph.getTElement();
            tElement.setText(leftContent);
            DefaultElement rElement = (DefaultElement) lastSubParagraph.getRElement();
            DefaultElement rightRElement = (DefaultElement) rElement.clone();
            Element rightTElement = rightRElement.element("t");
            rightTElement.setText(rightContent);
            elements.add(lastSubParagraph.getRIndex() + 2, rightRElement);
            super.addComment(wordComment, commentFile);
            super.doBindComment(firstSubParagraph.getRIndex() + 1, lastSubParagraph.getRIndex() + 3, lastSubParagraph.getRIndex() + 4, elements, wordComment);
        }
    }

    /**
     * 格式化子段落左边内容
     *
     * @param targetParagraph   被添加的目标段落
     * @param elements          段落的子段落xml元素集合
     * @param startIndex        添加批注句子的开始坐标
     * @param firstStartIndex   添加批注句子首次出现的子段落文字开始坐标
     * @param firstEndIndex     添加批注句子首次出现的段落文字结束坐标
     * @param firstSubParagraph 添加批注句子首次出现的子段落
     */
    private void leftParagraph(Paragraph targetParagraph, List<Element> elements, Integer startIndex, Integer firstStartIndex, Integer firstEndIndex, SubParagraph firstSubParagraph) {
        String targetParagraphContent = targetParagraph.getContent();
        String leftContent = targetParagraphContent.substring(firstStartIndex, startIndex);
        String rightContent = targetParagraphContent.substring(startIndex, firstEndIndex + 1);
        Element tElement = firstSubParagraph.getTElement();
        tElement.setText(leftContent);
        DefaultElement rElement = (DefaultElement) firstSubParagraph.getRElement();
        DefaultElement rightRElement = (DefaultElement) rElement.clone();
        Element rightTElement = rightRElement.element("t");
        rightTElement.setText(rightContent);
        elements.add(firstSubParagraph.getRIndex() + 1, rightRElement);
    }

    /**
     * 格式化子段落左边内容
     *
     * @param targetParagraph  被添加的目标段落
     * @param elements         段落的子段落xml元素集合
     * @param endIndex         添加批注句子的结束坐标
     * @param lastStartIndex   添加批注句子最后一次出现的子段落文字开始坐标
     * @param lastEndIndex     添加批注句子最后一次出现的段落文字结束坐标
     * @param lastSubParagraph 添加批注句子最后一次出现的子段落
     */
    private void rightParagraph(Paragraph targetParagraph, List<Element> elements, Integer endIndex, Integer lastStartIndex, Integer lastEndIndex, SubParagraph lastSubParagraph) {
        String targetParagraphContent = targetParagraph.getContent();
        String leftContent = targetParagraphContent.substring(lastStartIndex, endIndex + 1);
        String rightContent = targetParagraphContent.substring(endIndex + 1, lastEndIndex + 1);
        Element tElement = lastSubParagraph.getTElement();
        tElement.setText(leftContent);
        DefaultElement rElement = (DefaultElement) lastSubParagraph.getRElement();
        DefaultElement rightRElement = (DefaultElement) rElement.clone();
        Element rightTElement = rightRElement.element("t");
        rightTElement.setText(rightContent);
        elements.add(lastSubParagraph.getRIndex() + 1, rightRElement);
    }
}
